{include file="header"}
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            {if $item}
            <input type="hidden" name="id" value="{$item.id}">
            {/if}

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label"><span class="x-red">*</span>作品名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" value="{$item?$item.title:''}" lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入中文名称">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="title_jp" value="{$item?$item.title_jp:''}" lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入日文名称">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label"><span class="x-red">*</span>原作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="author" value="{$item?$item.author:''}" lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入中文名称">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="author_jp" value="{$item?$item.author_jp:''}" lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入日文名称">
                    </div>
                </div>

            </div>
            <div class="layui-form-item">

                <label class="layui-form-label"><span class="x-red">*</span>制作公司</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" onclick="selcompany()">点击选择</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red"></span></label>
                <div class="layui-input-inline">
                    <table class="layui-table" id="companylist">
                        {if $item}
                        {volist name="companylist" id="com"}
                        <tr class="kid_{$com.id}" data-id="">
                            <td><img src="{$com.logo|formatImg}" alt="" width="50"> </td>
                            <td>{$com.title_cn}</td>
                            <td>
                                <div style="display: none;"><input type="checkbox" style="" name="company" lay-skin="primary" value="{$com.id}"  checked></div>
                                <a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delcompany(this)">删除</a>
                            </td>
                        </tr>
                        {/volist}
                        {/if}
                    </table>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>作品形式</label>
                <div class="layui-input-block">
                    <input type="checkbox" lay-filter="work_form" name="work_form" value="1" title="TV" {if $item && in_array(1,$item['work_form'])}checked{/if}>
                    <input type="checkbox" lay-filter="work_form" name="work_form" value="2" title="漫画" {if $item && in_array(2,$item['work_form'])}checked{/if}>
                    <input type="checkbox" lay-filter="work_form" name="work_form" value="3" title="电影" {if $item && in_array(3,$item['work_form'])}checked{/if}>
                    <input type="checkbox" lay-filter="work_form" name="work_form" value="4" title="OVA" {if $item && in_array(4,$item['work_form'])}checked{/if}>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline comics_status_box" {if (!$item) || !in_array(2,$item['work_form'])}style="display: none;"{/if}>
                    <label class="layui-form-label"><span class="x-red">*</span>漫画更新状态</label>
                    <div class="layui-input-inline" style="width: 174px">
                        {if $item}
                        <input type="radio" name="comics_status" lay-filter="comics_status" value="1" title="连载中" {if $item.comics_status eq 1}checked{/if}>
                        <input type="radio" name="comics_status" lay-filter="comics_status" value="2" title="已完结" {if $item.comics_status eq 2}checked{/if}>
                        {else}
                        <input type="radio" name="comics_status" lay-filter="comics_status" value="1" title="连载中" checked>
                        <input type="radio" name="comics_status" lay-filter="comics_status" value="2" title="已完结">
                        {/if}
                    </div>
                    <div class="comics_all_box other_box" {if !$item || !$item.comics_all}style="display: none;"{/if}>
                        <span>全</span><input type="text" name="comics_all" value="{$item?$item['comics_all']:''}" autocomplete="off" class="other" placeholder=""><span>话</span>
                    </div>
                </div>
                <div class="layui-inline cartoon_status_box" {if (!$item) || !in_array(1,$item['work_form'])}style="display: none;"{/if}>
                    <label class="layui-form-label"><span class="x-red">*</span>TV更新状态</label>
                    <div class="layui-input-inline" style="width: 174px;">
                        {if $item}
                        <input type="radio" name="cartoon_status" lay-filter="cartoon_status" value="1" title="连载中" {if $item.comics_status eq 1}checked{/if}>
                        <input type="radio" name="cartoon_status" lay-filter="cartoon_status" value="2" title="已完结" {if $item.comics_status eq 2}checked{/if}>
                        {else}
                        <input type="radio" name="cartoon_status" lay-filter="cartoon_status" value="1" title="连载中" checked>
                        <input type="radio" name="cartoon_status" lay-filter="cartoon_status" value="2" title="已完结">
                        {/if}

                    </div>
                    <div class="cartoon_all_box other_box" {if !$item || !$item.cartoon_all}style="display: none;"{/if}>
                        <span>全</span><input type="text" name="cartoon_all" value="{$item?$item['cartoon_all']:''}"  autocomplete="off" class="other" placeholder=""><span>话</span>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">故事概要</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="desc" class="layui-textarea">{$item?$item.desc:''}</textarea>
                </div>
            </div>

            <div class="footer-btn">
                <input type="hidden" name="display" value="0">
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    提交
                </button>
                <button  class="layui-btn layui-btn-primary" type="button" lay-filter="temporary" lay-submit="">
                    暂存
                </button>
                <button  class="layui-btn layui-btn-primary cancel" type="button">
                    取消
                </button>
            </div>

        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer','upload'], function() {
        var form = layui.form,
            layer = layui.layer;

        //监听多选
        form.on('checkbox(work_form)', function(data){

            if(data.elem.checked && data.value==1){
                $('.cartoon_status_box').show()
            }
            if(!data.elem.checked && data.value==1){
                $('.cartoon_status_box').hide()
                $('[name="cartoon_status"]').val(1)
                $('.cartoon_all_box').hide()
                $('[name="cartoon_all"]').val(0)

            }
            if(data.elem.checked && data.value==2){
                $('.comics_status_box').show()
            }
            if(!data.elem.checked && data.value==2){
                $('.comics_status_box').hide()
                $('[name="comics_status"]').val(1)
                $('.comics_all_box').hide()
                $('[name="comics_all"]').val(0)
            }
        });
        form.on('radio(comics_status)', function(data){
            if(data.value == 1){
                $('.comics_all_box').hide()
                $('[name="comics_all"]').val(0)
            }else{
                $('.comics_all_box').show()
            }
        });
        form.on('radio(cartoon_status)', function(data){
            if(data.value == 1){
                $('.cartoon_all_box').hide()
                $('[name="cartoon_all"]').val(0)
            }else{
                $('.cartoon_all_box').show()
            }
        });
        form.on('submit(temporary)',function (data) {
            let postdata = getfield(data)
            postdata.field.display=1
            request("POST",'{:url("cartoon/post")}',postdata.field,function (res,msg) {
                //发异步，把数据提交给php
                layer.alert(msg, {icon: 6}, function() {
                    xadmin.close();
                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                });
            })

            return false;
        })

        //监听提交
        form.on('submit(add)', function(data) {
            let postdata = getfield(data)
            request("POST",'{:url("cartoon/post")}',postdata.field,function (res,msg) {
                //发异步，把数据提交给php
                layer.alert(msg, {icon: 6}, function() {
                    xadmin.close();
                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                });
            })

            return false;
        });
    })
    function getfield(data) {
        var company_ids = []
        $('[name="company"]').each(function () {
            if($(this).is(':checked')){
                company_ids.push($(this).val())
            }
        })
        var work_form = []
        $('[name="work_form"]').each(function () {
            if($(this).is(':checked')){
                work_form.push($(this).val())
            }
        })

        data.field.company_ids = company_ids
        data.field.work_form = work_form
        return data;
    }
    function addcompany(id,title,logo) {

        if($('body').find('.kid_'+id).length<=0){
            var html = "";
            html +=`<tr class="kid_${id}" data-id="${id}">`
            html +=`<td><img src="${logo}" alt="" width="50"> </td>`
            html +=`<td>${title}</td>`
            html +=`<td>`
            html +=`<div style="display: none;"><input type="checkbox" style="display: none" name="company" lay-skin="primary" value="${id}"  checked></div>`
            html +=`<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delcompany(this)">删除</a>`
            html +=`</td>`
            html +=`</tr>`
            $('#companylist').append(html)
        }
    }
    function delcompany(obj) {
        $(obj).parent().parent().remove()
    }
    function selcompany() {
        xadmin.open("选择制作公司",'{:url("company/index",["sel"=>true])}')
    }
</script>
{include file="footer"}